package com.sunshine.lnuplus.utils

import android.content.Context
import jxl.Workbook
import java.io.InputStream


/**
 * https://blog.csdn.net/u014743890/article/details/91045033
 */

fun readExcel(context: Context): ArrayList<Map<String, String>>? {
    try {
        val `is`: InputStream = context.assets.open("lesson.xls")
        val book = Workbook.getWorkbook(`is`)
        book.numberOfSheets
        val sheet = book.getSheet(0)
        val Rows = sheet.rows
        val arrayList = ArrayList<Map<String, String>>()
        var lessonId = ""
        var lessonName = ""
        var teacher = ""
        var banji = ""

        for (i in 1 until Rows) {
            val map = HashMap<String, String>()
            if(sheet.getCell(3, i).contents.isNotBlank()){
                lessonId = sheet.getCell(3, i).contents
                if(lessonId.isBlank()) lessonId = "000000"
                lessonName = sheet.getCell(5, i).contents
                if(lessonName.isBlank()) lessonName = "未知"
                teacher = sheet.getCell(8, i).contents
                if(teacher.isBlank()) teacher = "未知"
                banji = sheet.getCell(13, i).contents
                if(banji.isBlank()) banji = "00未知"
            }
            map.apply {
                var dayOfWeek= if(sheet.getCell(9, i).contents.isBlank()) "未知" else sheet.getCell(9, i).contents
                var jie = if(sheet.getCell(10, i).contents.isBlank()) "未知" else sheet.getCell(10, i).contents
                var weeks = if(sheet.getCell(11, i).contents.isBlank()) "未知" else sheet.getCell(11, i).contents
                var location = if(sheet.getCell(12, i).contents.isBlank()) "未知" else sheet.getCell(12, i).contents
                set("lessonId", lessonId)
                set("lessonName", lessonName)
                set("teacher", teacher)
                set("dayOfWeek", dayOfWeek)
                set("jie", jie)
                set("weeks", weeks)
                set("location", location)
                set("banji", banji)
            }
            arrayList.add(map)
        }
        book.close()
        return arrayList
    } catch (e: Exception) {
        return null
    }
}